<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="context-上下文" tabindex="-1"><a class="header-anchor" href="#context-上下文"><span>Context 上下文</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#how-it-works">工作原理</a></li>
</ul>
</li>
<li><a href="#capturing-context">捕获 Context 上下文</a>
<ul>
<li><a href="#stacks">堆栈</a></li>
</ul>
</li>
<li><a href="#retrieving-context">检索 Context 上下文</a>
<ul>
<li><a href="#determining-item-existence">确定项目是否存在</a></li>
</ul>
</li>
<li><a href="#removing-context">移除 Context 上下文</a></li>
<li><a href="#hidden-context">隐藏 Context 上下文</a></li>
<li><a href="#events">事件</a>
<ul>
<li><a href="#dehydrating">脱水</a></li>
<li><a href="#hydrated">注水</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Laravel 的「Context 上下文」功能使你能够在应用程序中执行的请求、任务和命令中捕获、检索和共享信息。这些捕获的信息也包含在应用程序写入的日志中，使你深入了解在写入日志条目之前发生的代码执行历史，并允许你跟踪整个分布式系统的执行流程。</p>
<h3 id="工作原理" tabindex="-1"><a class="header-anchor" href="#工作原理"><span>工作原理</span></a></h3>
<p>理解 Laravel Context 上下文功能的最佳方式是使用内置的日志功能来观察它的实际情况。首先，你可以使用 <code v-pre>Context</code> Facade <a href="#capturing-context">向 Context 上下文添加信息</a>。在此示例中，我们将使用 <a href="https://learnku.com/docs/laravel/11.x/middleware" target="_blank" rel="noopener noreferrer">中间件</a> 将请求 URL 和唯一的跟踪 ID 添加到每个传入请求的 Context 上下文中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Middleware</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>HttpFoundation<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AddContext</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理传入的请求</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Response</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'trace_id'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>添加到 Context 上下文中的信息会自动作为元数据附加到在请求期间写入的任何 <a href="https://learnku.com/docs/laravel/11.x/logging" target="_blank" rel="noopener noreferrer">日志条目</a> 中。将 Context 上下文作为元数据附加到日志条目中，可以将传递到单个日志条目的信息与通过 <code v-pre>Context</code> 共享的信息区分开来。例如，假设我们写入以下日志条目：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Log</span><span class="token operator">::</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User authenticated.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'auth_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>写入的日志将包含传递给日志条目的 <code v-pre>auth_id</code>，但也将包含上下文的 <code v-pre>url</code> 和 <code v-pre>trace_id</code> 作为元数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">User authenticated<span class="token operator">.</span> <span class="token punctuation">{</span><span class="token string double-quoted-string">"auth_id"</span><span class="token punctuation">:</span><span class="token number">27</span><span class="token punctuation">}</span> <span class="token punctuation">{</span><span class="token string double-quoted-string">"url"</span><span class="token punctuation">:</span><span class="token string double-quoted-string">"https://example.com/login"</span><span class="token punctuation">,</span><span class="token string double-quoted-string">"trace_id"</span><span class="token punctuation">:</span><span class="token string double-quoted-string">"e04e1a11-e75c-4db3-b5b5-cfef4ef56697"</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>添加到上下文的信息也可用于分发到队列的任务。比如，假设我们在将一些信息添加到上下文后，将 <code v-pre>ProcessPodcast</code> 任务分发给队列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 在中间件中...</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'trace_id'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在控制器中...</span></span>
<span class="line"><span class="token class-name static-context">ProcessPodcast</span><span class="token operator">::</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token variable">$podcast</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>任务分发时，当前保存在上下文中的所有信息，都可以在任务中捕获和分享。然后，在执行任务时，这些捕获的信息填充回到当前上下文中。因此，如果我们的任务的 handle 方法是写入日志：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ProcessPodcast</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Dispatchable</span><span class="token punctuation">,</span> InteractsWithQueue<span class="token punctuation">,</span> Queueable<span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Execute the job.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Log</span><span class="token operator">::</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Processing podcast.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'podcast_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">podcast</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>生成的日志条目将包含在最初调度该任务的请求期间添加到上下文中的信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Processing podcast<span class="token operator">.</span> <span class="token punctuation">{</span><span class="token string double-quoted-string">"podcast_id"</span><span class="token punctuation">:</span><span class="token number">95</span><span class="token punctuation">}</span> <span class="token punctuation">{</span><span class="token string double-quoted-string">"url"</span><span class="token punctuation">:</span><span class="token string double-quoted-string">"https://example.com/login"</span><span class="token punctuation">,</span><span class="token string double-quoted-string">"trace_id"</span><span class="token punctuation">:</span><span class="token string double-quoted-string">"e04e1a11-e75c-4db3-b5b5-cfef4ef56697"</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>尽管我们关注的是 Laravel 上下文的内置日志相关功能，但后续文档将说明上下文如何允许跨 HTTP 请求/队列任务边界共享信息，甚至如何添加未使用日志条目编写的<a href="#hidden-context">隐藏上下文数据</a>。</p>
<h2 id="捕获上下文" tabindex="-1"><a class="header-anchor" href="#捕获上下文"><span>捕获上下文</span></a></h2>
<p>使用 <code v-pre>Context</code> 门面的 <code v-pre>add</code> 方法，你可以在当前上下文中存储信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要一次添加多个项目，你可以将关联数组传递给 <code v-pre>add</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'first_key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'second_key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该 <code v-pre>add</code> 方法将覆盖共享同一个键名的现有值。如果你只想将还不存在键名的信息添加到上下文中，你可以使用 <code v-pre>addIf</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// "first"</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">addIf</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// "first"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="条件上下文" tabindex="-1"><a class="header-anchor" href="#条件上下文"><span>条件上下文</span></a></h4>
<p><code v-pre>when</code> 方法可以用于基于给定条件将数据添加到上下文中。如果给定的条件等于 <code v-pre>true</code>，<code v-pre>when</code> 方法中的第一个闭包将被调用；而如果给定的条件等于 <code v-pre>false</code>，则会调用第二个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">when</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$context</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'permissions'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">permissions</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$context</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'permissions'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="堆栈" tabindex="-1"><a class="header-anchor" href="#堆栈"><span>堆栈</span></a></h3>
<p>上下文提供了创建「堆栈」的能力，堆栈是按添加顺序存储的数据列表。你可以通过调用 <code v-pre>push</code> 方法将信息添加到堆栈中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'breadcrumbs'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'first_value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'breadcrumbs'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second_value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'third_value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'breadcrumbs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// [</span></span>
<span class="line"><span class="token comment">//     'first_value',</span></span>
<span class="line"><span class="token comment">//     'second_value',</span></span>
<span class="line"><span class="token comment">//     'third_value',</span></span>
<span class="line"><span class="token comment">// ]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>堆栈可用于捕获有关请求的历史信息，例如整个应用中发生的事件。比如，你可以创建一个事件监听器，以便在每次执行查询时推送到堆栈，将查询 SQL 和持续时间捕获为元组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'queries'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">time</span><span class="token punctuation">,</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">sql</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="检索上下文" tabindex="-1"><a class="header-anchor" href="#检索上下文"><span>检索上下文</span></a></h2>
<p>你可以使用 <code v-pre>Context</code> 门面的 <code v-pre>get</code> 方法，从上下文中检索信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>only</code> 方法可用于在上下文中检索信息子集。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first_key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second_key'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>pull</code> 方法可用于从上下文中检索信息并立即将其从上下文中删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想检上下文中存储的所有信息，你可以调用 <code v-pre>all</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="检查项目是否存在" tabindex="-1"><a class="header-anchor" href="#检查项目是否存在"><span>检查项目是否存在</span></a></h3>
<p>你可以使用 <code v-pre>has</code> 方法检测上下文是否为给定的键存有任何值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不管存储的值是什么，<code v-pre>has</code> 方法都将返回 <code v-pre>true</code>。因此，比如，具有 <code v-pre>null</code> 值的键将被视为存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="删除上下文" tabindex="-1"><a class="header-anchor" href="#删除上下文"><span>删除上下文</span></a></h2>
<p><code v-pre>forget</code> 方法可用于在当前上下文中删除键及它的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first_key'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second_key'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first_key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['second_key' => 2]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过给 <code v-pre>forget</code> 方法提供一个数组，你可以一次性删除多个键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first_key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second_key'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="隐藏上下文" tabindex="-1"><a class="header-anchor" href="#隐藏上下文"><span>隐藏上下文</span></a></h2>
<p>上下文提供存储隐藏数据的能力。这些隐藏信息不会追加到日志中，也无法通过上述数据检索方法进行访问。上下文提供了一组不同的方法来与隐藏的上下文信息交互：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">addHidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">getHidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// 'value'</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// null</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这些「隐藏」方法对上述非隐藏方法的功能进行了镜像：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">addHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">addHiddenIf</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">pushHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">getHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">pullHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">onlyHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">allHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">hasHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">forgetHidden</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>上下文分发了两个事件，允许你插入钩子到上下文的注水和脱水处理过程。</p>
<p>为了说明如何使用这些事件，假设在应用的中间件中，你根据传入的HTTP 请求的 <code v-pre>Accept Language</code> 标头设置 <code v-pre>app.locate</code> 配置值。上下文的事件允许你在请求期间捕获此值并将其还原到队列中，从而确保在队列上发送的通知具有正确的 <code v-pre>app.locale</code> 值。我们可以使用上下文的事件和<a href="#hidden-context">隐藏的</a> 数据来实现这一点，下面的文档将对此进行说明。</p>
<h3 id="脱水" tabindex="-1"><a class="header-anchor" href="#脱水"><span>脱水</span></a></h3>
<p>每当任务被调度到队列时，上下文中的数据都会被「脱水」，并与任务业的有效负载一起捕获。<code v-pre>Context::dehydrating</code> 方法允许你对将在脱水过程中调用的闭包进行注册。在此闭包中，你可以对将与队列任务共享的数据进行更改。</p>
<p>通常，你应该在应用的 <code v-pre>AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中注册 <code v-pre>dehydrating</code> 回调：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Log<span class="token punctuation">\</span>Context<span class="token punctuation">\</span>Repository</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Config</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Bootstrap any application services.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">dehydrating</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Repository</span> <span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$context</span><span class="token operator">-></span><span class="token function">addHidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'locale'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Config</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.locale'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意<br>
你不应在 <code v-pre>dehydrating</code> 回调中使用 <code v-pre>Context</code> 门面，因为这会将修改当前进程的上下文。请确保仅对传递给回调的存储库进行更改。</p>
</blockquote>
<h3 id="注水" tabindex="-1"><a class="header-anchor" href="#注水"><span>注水</span></a></h3>
<p>每当队列中的任务开始在队列上执行时，与该任务共享的任何上下文都将注水回当前上下文。<code v-pre>Context::hydrated</code> 方法允许你注册将在注水过程中调用的闭包。</p>
<p>通常，你应该在应用的 <code v-pre>AppServiceProvider</code> 类中的 <code v-pre>boot</code> 方法里注册 <code v-pre>hydrated</code> 回调：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Log<span class="token punctuation">\</span>Context<span class="token punctuation">\</span>Repository</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Config</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Context</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Bootstrap any application services.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Context</span><span class="token operator">::</span><span class="token function">hydrated</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Repository</span> <span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$context</span><span class="token operator">-></span><span class="token function">hasHidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'locale'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token class-name static-context">Config</span><span class="token operator">::</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.locale'</span><span class="token punctuation">,</span> <span class="token variable">$context</span><span class="token operator">-></span><span class="token function">getHidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'locale'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注<br>
你不应在<code v-pre>hydrated</code> 回调中使用 <code v-pre>Context</code> 门面，并请确保仅对传递给回调的存储库进行更改。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/contextmd/16675" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/co...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/contextmd/16675" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/co...</a></p>
</blockquote>
</div></template>


